package com.tron.sync.controller;

import com.tron.sync.elk.AmqpMessageReceive;
import com.tron.sync.entities.SyndaOrgDoc;
import com.tron.sync.entities.SyndaUserDoc;
import com.tron.sync.service.ElkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.util.*;

@Api(tags = "全文检索")
@RestController
public class ElkController {

    @Autowired
    AmqpMessageReceive amqpMessageReceive;

    @Autowired
    ElkService elkService;

    @ApiOperation(value = "全文检索统计接口")
    @GetMapping("/count")
    public Object count(@RequestParam String orgId) {
        final HashMap<String, Object> result = new HashMap<>();
        final HashMap<String, Object> dataResult = new HashMap<>();
        try {
            final Map<String, Object> count = new HashMap<>();
            final Instant start = Instant.now();
            final HashMap<String, Long> countUser = elkService.countUser(orgId);
            final HashMap<String, Long> countOrg = elkService.countOrg(orgId);
            final ArrayList<Map<String, Object>> countOrgUser = elkService.countOrgUser(orgId);
            final Instant end = Instant.now();
            count.put("countUser", countUser);
            count.put("countOrg", countOrg);
            count.put("countOrgUser", countOrgUser);
            dataResult.put("data",count);
            result.put("state", 200);
            result.put("message", "success");
            result.put("data",dataResult );
            result.put("useTime", Duration.between(start,end).toMillis());
        } catch (Exception e) {
            result.put("state", 500);
            result.put("message", "全文检索服务异常");
            result.put("reason", e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "同步全量数据到全文检索")
    @GetMapping("/init")
    public Object init() throws ParseException {
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Date date = simpleDateFormat.parse("1970-01-01 00:00:00");
        amqpMessageReceive.sync(date);
        return "init end";
    }

    @ApiOperation(value = "清空全文检索用户，机构数据")
    @GetMapping("/clean")
    public Object clean() {
        elkService.cleanUser();
        elkService.cleanOrg();
        return "clean end";
    }

    @ApiOperation(value = "获取映射配置")
    @GetMapping("/getMapping")
    public Object getMapping() {
        final Object mapping = elkService.getMapping();
        return mapping;
    }

    @ApiOperation(value = "删除索引")
    @GetMapping("/deleteIndex")
    public Object deleteIndex() {
        final Object o = elkService.deleteIndex();
        return o;
    }

    @ApiOperation(value = "根据拼音吗查询机构和用户")
    @GetMapping("/getUserAndOrgByPinYin")
    public Object getUserAndOrgByPinYin(String orgId,String pinyin,String user1org2userAndOrg3) {
        final HashMap<String, Object> result = new HashMap<>();
        final HashMap<String, Object> dataResult = new HashMap<>();
        try {
            final List<SyndaOrgDoc> orgByPinYin = elkService.getOrgByPinYin(orgId, pinyin);
            final ArrayList<SyndaUserDoc> userByPinYin = elkService.getUserByPinYin(orgId, pinyin);
            final HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("users", userByPinYin);
            hashMap.put("orgs", orgByPinYin);
            dataResult.put("data",hashMap);
            result.put("state", 200);
            result.put("message", "success");
            result.put("data",dataResult );
        } catch (Exception e) {
            result.put("state", 500);
            result.put("message", "全文检索服务异常");
            result.put("reason", e.getMessage());
        }
        return result;
    }


}
